package cn.cmcc.online.smsapi.sdk.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.AlarmClock;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.widget.Toast;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.dex.DexUtil;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
class ActionHelper {
	/**
	 * /* 发送邮件
	 */
	public static void sendEmailTo(Context context, String emailAddr, Map extend) {
        String mailtoAction = String.format("mailto:%s", emailAddr);
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse(mailtoAction));
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            /*SDK-536 hewengao/20160713 start*/
            LogManager.w(Constant.TAG, "ActionHelper sendEmailTo",e);
            /*SDK-536 hewengao/20160713 end*/
		}
	}

	/*
	 * 
	 */
	@SuppressWarnings("deprecation")
	public static void copyCode(Context context, String code) {
		ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		cmb.setText(code);
		Toast.makeText(context, "已复制到剪贴板", Toast.LENGTH_LONG).show();
	}

	/**
	 * 添加联系人
	 */
	public static void addContact(Context context, String name, String phone, String email,String url, Map extend) {
	    Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
		if (!TextUtils.isEmpty(name)) {
			intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
		}
		/*SDK-670 zhaojiangwei 20161108 start */
		ArrayList<ContentValues> data = new ArrayList<ContentValues>();
		if (!TextUtils.isEmpty(phone)) {
		    ContentValues row = new ContentValues();
            row.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            row.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
            row.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone);
            data.add(row);
		}
		if (!TextUtils.isEmpty(email)) {
            ContentValues row = new ContentValues();
            row.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
            row.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);
            row.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
            data.add(row);
		}
		if (!TextUtils.isEmpty(url)) {
	        ContentValues row = new ContentValues();
	        row.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
	        row.put(ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_WORK);
	        row.put(ContactsContract.CommonDataKinds.Website.URL, url);
	        data.add(row);   
        }
		intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
		/*SDK-670 zhaojiangwei 20161108 end */
		try {
			context.startActivity(intent);
		}
		catch (Exception e) {
			/*SDK-411 zhaojiangwei 20160713 start */
			LogManager.w(Constant.TAG, "ActionHelper.addContact exception:", e);
			/*SDK-411 zhaojiangwei 20160713 end */
		}
	}

	/**
	 * 添加闹铃
	 * 
	 */
	public static void addAlarm(Context context, int hour, int minute, Map extend) {
		Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
		intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
		intent.putExtra(AlarmClock.EXTRA_MINUTES, minute);
		intent.putExtra(AlarmClock.EXTRA_SKIP_UI, false);
		try {
			context.startActivity(intent);
		}
		catch (Exception e) {
			/*SDK-411 zhaojiangwei 20160713 start */
			LogManager.w(Constant.TAG, "ActionHelper.addAlarm exception:", e);
			/*SDK-411 zhaojiangwei 20160713 end */
		}
	}

	/**
	 * 查看日历
	 * 
	 */
	@SuppressLint("NewApi")
	public static void viewCalendar(Context context, long timestamp, Map extend) {
		Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
		builder.appendPath("time");
		ContentUris.appendId(builder, timestamp);
		Intent intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
		try {
			context.startActivity(intent);
		}
		catch (Exception e) {
            /*SDK-536 hewengao/20160713 start*/
            LogManager.w(Constant.TAG, "ActionHelper viewCalendar",e);
            /*SDK-536 hewengao/20160713 end*/
		}
	}

	/**
	 * 添加书签到系统默认浏览器
	 * 
	 */
	// public static boolean addBookMark(Context context, String url) {
	// boolean opRet = true;
	// ContentValues inputValue = new ContentValues();
	// inputValue.put(Browser.BookmarkColumns.BOOKMARK, "1");
	// inputValue.put(Browser.BookmarkColumns.TITLE, url);
	// inputValue.put(Browser.BookmarkColumns.URL, url);
	// ContentResolver cr = context.getContentResolver();
	// try {
	// Uri uri = cr.insert(Browser.BOOKMARKS_URI, inputValue);
	// if (uri==null) {
	// opRet = false;
	// }
	// }
	// catch (Exception e) {
	// // TODO: handle exception
	// }
	// return opRet;
	// }

	public static String convertTime(String date) {
		String time = date;
		String strHour = "时";
		String strMinute = "分";
		if (time.contains(strHour) && time.contains(strMinute)) {
			int pos = time.indexOf(strMinute);
			time = time.substring(0, pos);
			time = time.replaceAll(strHour, ":");
			LogManager.w("DoAction", time);
		}
		return time;
	}
	/*UIX标准方案UIX-207 kedeyuan 2016.06.17 start*/
	   public static String convertTimeToString(String strDate) {
	        String reTimeString="";
	        Date date = DexUtil.convertDate(strDate);
	        if (date!=null) {
	            SimpleDateFormat sfm= new SimpleDateFormat("HH:mm");
	            reTimeString=sfm.format(date);
	        }
	        return reTimeString;
	    }
	   /*UIX标准方案UIX-207 kedeyuan 2016.06.17 end*/

	/*
	 * 日期格式化转换
	 */
	public static long convertDate(String strDate) {
		long time = System.currentTimeMillis();
//		String formated = convertDateStr(strDate);
//		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//		try {
//			time = df.parse(formated).getTime();
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
		Date date = DexUtil.convertDate(strDate);
		if (date!=null) {
			time = date.getTime();
		}
		return time;
	}

	public static String convertDateStr(String date) {
		String strYear = "年";
		String strMonth = "月";
		String strDay = "日";

		String strHour = "时";
		String strMinute = "分";
		String formated = date;
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int month = Calendar.getInstance().get(Calendar.MONTH);
		int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		if (!TextUtils.isEmpty(date)) {

			if (date.contains(strMonth) && date.contains(strDay)) {
				int pos = date.indexOf(strDay);
				date = date.substring(0, pos);
				date = date.replaceAll(strMonth, "-");
				if (date.contains(strYear)) {
					date = date.replaceAll(strYear, "-");
				}
				else {
					date = String.format("%d-%s", year, date);
				}
				LogManager.w("DoAction", date);
			}
			if (date.contains(strHour) && date.contains(strMinute)) {
				int pos = date.indexOf(strMinute);
				date = date.substring(0, pos);
				date = date.replaceAll(strHour, "-");
				LogManager.w("DoAction", date);
			}
			if (date.contains(" ")) {
				int pos = date.lastIndexOf(" ");
				date = date.substring(0, pos);
			}
			if (date.contains("/")) {
				String temps[] = date.split("\\/");
				if (temps != null) {
					if (temps.length == 3) {
						year = Integer.parseInt(temps[0]);
						month = Integer.parseInt(temps[1]);
						day = Integer.parseInt(temps[2]);
					}
					else if (temps.length == 2) {
						month = Integer.parseInt(temps[0]);
						day = Integer.parseInt(temps[1]);
					}
				}
			}
			if (date.contains("-")) {
				String temps[] = date.split("-");
				if (temps != null) {
					if (temps.length == 3) {
						year = Integer.parseInt(temps[0]);
						month = Integer.parseInt(temps[1]);
						day = Integer.parseInt(temps[2]);
					}
					else if (temps.length == 2) {
						month = Integer.parseInt(temps[0]);
						day = Integer.parseInt(temps[1]);
					}
				}
			}
			formated = String.format("%d-%02d-%02d", year, month, day);
		}
		return formated;
	}
}
